package com.dzf.controller;

import com.dzf.entity.ItemBook;
import com.dzf.entity.Order;
import com.dzf.entity.RespJson;


import com.dzf.service.OrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author DZF
 * 订单头的管理模块
 */
@RestController
@RequestMapping("/api/adm/ord")
public class AdmOrdController {
    @Autowired
    OrdService ordService;

    /**
     * 添加订单头
     * @param ord 订单头的详细信息
     * @return 无data
     */
    @RequestMapping("/addShip")
    public RespJson addShip(Order ord){
        ordService.addShip(ord);
        return RespJson.pack(200,"OK",null);
    }

    /**
     * 对订单头进行分页查询
     * @param map 存放了订单头和用户的信息组合类
     * @param start 起始页
     * @param num   每页数据数
     * @return    查询结果
     */
    @RequestMapping("/findOrdInfo/{start}/{num}")
    public RespJson findOrdInfo(@RequestBody Map<String,Object> map, @PathVariable("start") int start, @PathVariable("num")int num)
    {
        //计算该页从第几行开始
        map.put("start",(start*num)-num);
        map.put("num",num);
        Map<String,Object> resultmap =  ordService.findOrdInfo(map);
        return RespJson.pack(200,"OK",resultmap);
    }


    /**
     * 查询订单商品明细
     * @param ordid 根据订单ID
     * @return
     */
    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    public RespJson findItemInfoByOrdid(@PathVariable("ordid") int ordid){
        List<ItemBook> itemBooks = ordService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200,"OK",itemBooks);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

